﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace Movistar.SMS.OutlookAddIn
{
    static class MovistarGateway
    {
        public static void SendMessage(string sServiceURL, string sServiceMobile, string sServicePass, string sTo, string sMsg)
        {
            try
            {
                string postData = "TM_ACTION=AUTHENTICATE&TM_LOGIN=" + sServiceMobile + "&TM_PASSWORD=" + sServicePass +
                    "&to=" + sTo + "&message=" + sMsg;

                WebRequest objRequest = WebRequest.Create(sServiceURL);
                objRequest.Timeout = 10000;
                objRequest.Method = "POST";
                objRequest.ContentLength = postData.Length;
                objRequest.ContentType = "application/x-www-form-urlencoded";

                StreamWriter postWriter = new StreamWriter(objRequest.GetRequestStream());
                postWriter.Write(postData);
                postWriter.Close();

                WebResponse objResponse = objRequest.GetResponse();
                StreamReader sr = new StreamReader(objResponse.GetResponseStream());
                string rawOutput = sr.ReadToEnd();
                sr.Close();

                if(rawOutput.Trim().ToUpper() == "OK")
                    MessageBox.Show("Mensaje de prueba enviado con éxito.", "Configuración Movistar SMS",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                MessageBox.Show("Ha ocurrido un error inesperado en el envío.\n" + rawOutput, "Configuración Movistar SMS",
                    MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
            catch
            {
                MessageBox.Show("Ha ocurrido un error inesperado en el envío.", "Configuración Movistar SMS",
                    MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
        }
    }
}
